$(function () {
    layui.use(['layer', 'form', 'layedit', 'util'], function () {
        var layer = layui.layer, form = layui.form, layedit = layui.layedit, util = layui.util
        util.fixbar({
            click: function (type) { }
        });
        // 初始化编辑器
        markdownToHtml()
        // 收藏
        $('.collection').click(function () {
            collection(layer)
        })
        form.render()
    })


    /**
     * 收藏文章
     * @param layer
     */
    function collection(layer) {
        var questionId = $('.question-id').text();
        $.ajax({
            url: '/collection/addSingle',
            type: 'post',
            data: {relationId: questionId, type: 1},
            dataType: 'json',
            success: function (response) {
                if (response.code === '000403') {
                    openLogin(layer)
                } else if (response.code === '000000') {
                    layer.msg('收藏成功...', {icon: 1});
                }
            }
        })
    }


    /**
     * Markdown转html
     */
    function markdownToHtml() {
        //先对容器初始化，在需要展示的容器中创建textarea隐藏标签，
        editormd.markdownToHTML("editor-content", {
            // htmlDecode: "style,script,iframe", //可以过滤标签解码
            // emoji: true,
            // taskList: true,
            // tex: true,               // 默认不解析
            // flowChart: true,         // 默认不解析
            // sequenceDiagram: true,  // 默认不解析
            // toc: true,
            // tocTitle: "目录",
            // selector: "h1,h2,h3,h4,h5,h6", //生成目录 时的选择器
            // tocContainer: "",
            // markdown: "",
            gfm: true,
            toc: true,
            tocm: true,
            tocStartLevel: 1,
            tocTitle: "目录",
            tocDropdown: false,
            tocContainer: "",
            markdown: "",
            markdownSourceCode: false,
            htmlDecode: false,
            autoLoadKaTeX: true,
            pageBreak: true,
            atLink: true,    // for @link
            emailLink: true,    // for mail address auto link
            tex: false,
            taskList: false,   // Github Flavored Markdown task lists
            emoji: false,
            flowChart: false,
            sequenceDiagram: false,
            previewCodeHighlight: true,
            selector: "h1,h2,h3,h4,h5,h6" //生成目录 时的选择器
        });
    }

})